var  Xml2js = require('xml2js')

var xmlParser = new Xml2js.Parser({explicitArray: false, ignoreAttrs: false});


var xml = '<xMl> <key> <value> ' +
  '<Aa>a</Aa><bb>b</bb>' +
  '</value> <value>' +
  '<Aa>aa</Aa><bb>bb</bb>' +
  '</value> <value2>1212</value2> </key> </xMl>'
var expression = 'Xml.key.value,"aa","aa","bb'
//var expression = 'xml.key.value2'
var updatevalue = '56755'

let promise = new Promise((resolve, reject) => {

  xmlParser.parseString(xml,function (err,rep) {
    if (null!= err){console.log(err);return}
    resolve(rep)
  })
})

promise.then(value => {

 // setValue(expression,value,updatevalue)
 // let searchvalue = findValue(expression,value)
 // console.log(searchvalue)
 // keyCalibration(expression,value)
  //console.log(JSON.stringify(value))
  //  keyCalibration(expression,value)
  // console.log(keyCalibration(expression,value))
})

console.log(expression.replace(/\"/g,''))

var findValue = function (expression,obj) {

  let arr = expression.replace(/\"/g,'').split(',')
  let j =obj

  if (arr.length > 1){// 'xml.key.value,"aa","a","bb'


    let searchkey = arr[1]
    let searchValue = arr[2]
    let changekey = arr[3]
    let subarr = arr[0].split('.');

    for (let i in subarr){
      j=j[subarr[i]]
    }
    console.log(j )
    for (let i in j){
      if (j[i][searchkey] == searchValue){

        j= j[i][changekey]
        break
      }
    }

  } else {    // xml.key.value2

    let subarr =   expression.split('.')

    for (let i in subarr){

      j=j[subarr[i]]
    }


  }

   return j
}


var setValue = function (expression,obj,updatevalue) {

  let arr = expression.replace(/\"/g,'').split(',')
  let j =obj

  if (arr.length > 1){              // 'xml.key.value,"aa","a","bb'
    let subarr = arr[0].split('.')

    for (let i in subarr){
      j=j[subarr[i]]
    }

    for (let i in j){
      let key = arr[1]
      let searchValue = arr[2]
      let changekey = arr[3]
      if (j[i][key] == searchValue){

        j[i][changekey] = updatevalue
        return
      }
    }

  } else {    // xml.key.value2

    let subarr =   expression.split('.')



    for (let i in subarr){


      if (i == subarr.length-1){
        j[subarr[i]]=updatevalue

       return
      }
      j=j[subarr[i]]
    }

  }

}

var keyCalibration = function (expression,obj) {  //根据  json中大小写 来校准  expression 中的key值

       let arr = expression.replace(/\"/g,'').split(',')
       let subarr = arr[0].split('.')
       let objtmp = obj

  for (let i in subarr){   //校准subarr
      let keys = getObjkey(objtmp)
       for(let j in keys){

         if (keys[j].toLowerCase() == subarr[i].toLowerCase()){
           subarr[i]=keys[j]
           objtmp = objtmp[keys[j]]
           break
         }


       }
  }

  //校准 二维数据
   if (arr.length >1){
     let flag =0
     let keys = getObjkey(objtmp[0])
       for (let j in keys){
         if (keys[j].toLowerCase() == arr[1]){
           arr[1] = keys[j]
           flag++

         }
         if (keys[j].toLowerCase() == arr[3]){
           arr[3] = keys[j]
           flag++
         }

         if (flag == 2 ){
           break
         }

       }

   }

 //重新组装
  let newEXpre = ''
  for (let index in subarr){
    if (subarr.length -1 == index) {
      newEXpre = newEXpre + subarr[index]
    }else{
      newEXpre = newEXpre + subarr[index]+'.'
    }


  }
  for(let i =1 ; i<arr.length ;i++){
    newEXpre = newEXpre +','+ arr[i]
  }



return newEXpre

}

var getObjkey = function (obj) {
  let arr=[]
  for (let i in obj){
    arr.push(i)
  }

return arr
}


/**
 * 删除json子key
 *
 */


let ob = {
  a:12,
  b:23,
  c:34,
  d:{
     e:5,
    d:{
       f:6,
      g:7
    }
  }

}
let expre = 'DSF.isNotExist(d.d.f)'

let reg = /DSF.isNotExist\((.*)\)/

console.log(reg.exec(expre)[1])

// let delkey = 'a'
//

// console.log(ob)
// delete ob['a']
// console.log(ob)



//CF.sumProp(ywbw.a107020.nyglxmGrid.xmGridlb,"msxmsde")



//deletearr(expre,ob,'')
//console.log(ob)

function deletearr(expression,obj,updatevalue) {




  if (expression.indexOf('DSF.isNotExist') != -1){
    console.log('删除节点')
    console.log(expression)
    isnotexistFlag = true
    expression = getIsNotExistRegexp(expression,/DSF.isNotExist\((.*)\)/)
    console.log(expression)
    let j =obj

    let subarr =   expression.split('.')
    for (let i in subarr){
      console.log(j)
      console.log(subarr[i])
      if (i == subarr.length -1   ){
        console.log('delete')
        console.log(j)
        delete j[subarr[i]]
        console.log(j)
        break
      }
      if (j[subarr[i]] != null){
        j = j[subarr[i]]

      }


    }





    return

  }

}

//DSF.isNotSubstring(a000000.qyjcxxb2018Form.tbbdqk,"A102020")

//console.log(getIsNotExistRegexp('DSF.isNotSubstring(a000000.qyjcxxb2018Form.tbbdqk,"A102020")',/DSF.isNotSubstring\((.*),/))

 function getIsNotExistRegexp(expression,reg){




  return  reg.exec(expression)[1]

  // console.log(reg.exec(expre)[1])


}
let array = [0,1,2,3,4]
console.log(JSON.stringify(array))

let obj = {aa:'efef'}
console.log(JSON.stringify(obj))

let name = '123_efefxfefefefd.xml'
  let newname = name.replace('123','567')
console.log(name)
console.log(newname)

//CF.sumProp(ywbw.a107020.nyglxmGrid.xmGridlb,"msxmsde")   /DSF.isNotExist\((.*)\)/
let sumprop = 'CF.sumProp(ywbw.a107020.nyglxmGrid.xmGridlb,"msxmsde")'
let rege = /CF.sumProp\((.*)\)/
console.log(rege.exec(sumprop)[1])


// DSF.isEqual(DSF.nvl(bblxdm,"A"),"A")
// -- DSF.isNotExist(xqylrbnb)
// SDS.getQylxFor2019(a000000.qyjcxxb2018Form.qysdsnbcjcwkjzdflDm)
// DSF.nvl({基础信息表A000000_其余股东合计投资比例},0)

let  str = 'SDS.getQylxFor2019(a000000.qyjcxxb2018Form.qysdsnbcjcwkjzdflDm)'
let regs = /SDS.getQylxFor2019\((.*)\)/
//let regs1 = /DSF.isEqual\((.*)\)/
console.log(regs.exec(str)[1])
//console.log(regs1.exec(str)[1])


//<![CDATA[" 开始，由 "]]>

let strcdta = '<![CDATA[" 开始，由 "]]>'
let rescdata = /<!\[CDATA\[(.*)\]\]>/
console.log(rescdata.exec(strcdta))




function parseJson(jsonObj,arr) {
  // 循环所有键
  for(var key in jsonObj) {
    //如果对象类型为object类型且数组长度大于0 或者 是对象 ，继续递归解析
    var element = jsonObj[key];
    if(element.length == null && typeof(element) == "object") {
      console.log("----eles -->  " + key + ":" + element + " ");
      parseJson(element,arr);
    }
    arr.push(key)

  }
}



let json = {aa:'123',bb:{cc:'efef',dd:[43434,5454,656]}}
let arr = []
parseJson(json,arr)
console.log(JSON.stringify(arr))

console.log(json.length)
console.log({aa:'12'}.length)
console.log(typeof ({aa:'12'}))

let vro = [{
  "id": 23607753,
  "ruleId": "21000000004",
  "varIndex": 1,
  "varName": "企业所得税年度纳税申报表A100000_资产减值损失",
  "varValue": "ywbw.a100000.zbForm.zcjzss"
}, {
  "id": 23607754,
  "ruleId": "21000000004",
  "varIndex": 2,
  "varName": "利润表_证券_资产减值损失本期金额",
  "varValue": "SDS.getValue(sywzxxzzzqgslrb.sywzxxzzzqgslrbGrid.zqgslrbGridlbVO,\"ewbhxh\",15,\"bqje\")"
}, {
  "id": 23606454,
  "ruleId": "41000000004",
  "varIndex": 1,
  "varName": "企业所得税年度纳税申报表A100000_资产减值损失",
  "varValue": "ywbw.a100000.zbForm.zcjzss"
}, {
  "id": 23606454,
  "ruleId": "41000000004",
  "varIndex": 1,
  "varName": "企业所得税年度纳税申报表A100000_资产减值损失",
  "varValue": "ywbw.a100000.zbForm.zcjzss"
}, {
  "id": 23606455,
  "ruleId": "41000000004",
  "varIndex": 2,
  "varName": "证券利润表不存在",
  "varValue": "DSF.isNotExist(sywzxxzzzqgslrb)"
}, {
  "id": 23606455,
  "ruleId": "41000000004",
  "varIndex": 2,
  "varName": "证券利润表不存在",
  "varValue": "DSF.isNotExist(sywzxxzzzqgslrb)"
}]

function arrObjectDistinct(arr,objkey){ // 根据对arr中 obj 的一个属性 去重
  let existkey = {}
  let newarr = []
  for(let i in arr){

    if (!existkey[arr[i][objkey]]){
      existkey[[arr[i][objkey]]] = true
      newarr.push(arr[i])
    }
  }
  return newarr;

}

console.log(arrObjectDistinct(vro,'varName'))


function deleteArrObj(arr,objkey,objkeyvalue){
  let arrtemp = []
  for (let i in arr){
    console.log(arr[i][objkey] == objkeyvalue)
    if (arr[i][objkey] != objkeyvalue){
      arrtemp.push(arr[i])
    }
  }
  return arrtemp
}

console.log('==========')
console.log(deleteArrObj(vro,'id','23606454'))


let jsonxml = {aa:'bb',bb:'bb',cc:{ee:'efef'}}
console.log(jsonxml.cc[0])



console.log(jsonxml.length == null)

 let wxsf= [{"reslutValue":1,"paraValues":null,"createDate":"2020-02-12 21:24:00.0"}]
console.log(wxsf.paraValues == null)
console.log(wxsf.paraValues != null)
console.log(null != '')





function  getIsNotExistRegexp(expression,reg){
  console.log(expression)
  console.log(reg)



  return  reg.exec(expression)[1]

  // console.log(reg.exec(expre)[1])


}


